describe("VNCClient",function() {
	var cl = null;
	it("Can be created", function() {
		cl = new VNCClient();
		expect(cl).toBeDefined();
	});
	
	it("Can connect to a VNC Websocket server",function() {
		cl.host = "127.0.0.1" || prompt("Enter the host of the VNC Websocket server");
		cl.port = "2000" || prompt("Enter the port of the VNC Websocket server");
	
		var socketFn = false;
		cl.socket.registerEvent("onopen",function() {socketFn = true;},this,true);
		cl.socket.registerEvent("onerror",function() {socketFn = true;},this,true);
		
		var onOpenSpy = spyOn(cl,"openFn").andCallThrough(); 
		var onErrorSpy = spyOn(cl,"errorFn").andCallThrough();
		
		runs(function() {
			cl.connect();	
		});
		
		waitsFor(function() {
			return socketFn;
		},"Connecting to server", 10000);
		
		runs(function() {	
			expect(onErrorSpy).not.toHaveBeenCalled()
			expect(onOpenSpy).toHaveBeenCalled()
		});
	});
	
	xit("Can connect to a secure VNC Websocket server",function() {});
	
	it("Can send framebuffer update requests",function() {
		var onSendSpy = spyOn(cl.socket,"send")
		cl.requestFBUpdate();
		expect(onSendSpy).toHaveBeenCalledWith("FramebufferUpdate;EOF");
	});
	
	it("Can receive dimension updates",function() {
		var dimensions = false;
		runs(function() {
			cl.registerEvent("dimensionsChanged",function(dim) {
				dimensions = dim;
			},this);
			var data = '{"type": "updateDimensions","width":412, "height" : 421}';
			cl.socket.__Socket.onmessage({
				data: data
			});
		});
		waitsFor(function() {
			return dimensions != false;		
		},"Timeout occured",1000);
		runs(function() {
			expect(cl.fbDimensions.width).toEqual(412);
			expect(cl.fbDimensions.height).toEqual(421);
		});
	});

	it("Can receive framebuffer update responses",function() {
		var respReceived = false;
		var onRecvSpy = spyOn(cl,"handleResponse").andCallFake(function() {
			respReceived = true;

		});
		cl.registerEvents();
		var data = '{"type": "framebufferUpdate",'+
			'"x":113,"y":155,"width":142, "height" : 152,'+
			'"data" : "0000", "encoding" : "test"}'

		runs(function() {
			cl.socket.__Socket.onmessage({
				data: data
			});	
		});
		waitsFor(function() {
			return respReceived == true;				
		},"Framebuffer request",1000)
		runs(function() {
			expect(onRecvSpy).toHaveBeenCalledWith(data);			
		})		
	});



	it("Can send pointer position update",function() {
		var onSendSpy = spyOn(cl.socket,"send")
		cl.sendPointerUpdate({pageX:300,pageY:200,which:0});
		expect(onSendSpy).toHaveBeenCalledWith("PointerEvent;300;200;0;EOF");
	});
	it("Can leftclick",function() {
		var onSendSpy = spyOn(cl.socket,"send")
		cl.sendPointerUpdate({pageX:300,pageY:200,which:1});
		expect(onSendSpy).toHaveBeenCalledWith("PointerEvent;300;200;1;EOF");		
	});
	it("Can middleclick",function() {
		var onSendSpy = spyOn(cl.socket,"send")
		cl.sendPointerUpdate({pageX:300,pageY:200,which:2});
		expect(onSendSpy).toHaveBeenCalledWith("PointerEvent;300;200;2;EOF");		
	});
	it("Can rightclick",function() {
		var onSendSpy = spyOn(cl.socket,"send")
		cl.sendPointerUpdate({pageX:300,pageY:200,which:3});
		expect(onSendSpy).toHaveBeenCalledWith("PointerEvent;300;200;4;EOF");		
	});

	
	it("Sending keyevents - ASCII",function() {
		expect(false).toEqual(true);		
	})
	
	it("Sending keyevents - Control chars", function() {
		expect(false).toEqual(true);
	})
	
	it("Can update cutText",function() {
		expect(false).toEqual(true);
	});
	
	it("Can receive cutText",function() {
		expect(false).toEqual(true);
	});
})
